$().ready(function () {
//	$('.dataTables-example').dataTable();
	var table = $('.branchChannelTable');
	var oTable = table.dataTable({
        "lengthMenu": [
               [10,20,30,40,50,100],
               [10,20,30,40,50,100]
        ],
        "pageLength": 10,
        "language": {
        	"processing": "数据加载中...",
            "emptyTable": "暂无数据",
            "info": "第_START_到_END_条记录,共_TOTAL_条记录",
            "infoEmpty": "没有找到记录",
            "infoFiltered": "",
            "lengthMenu": "每页显示 _MENU_ 条记录",
            "search": "搜索:",
            "zeroRecords": "未找到匹配记录",
            "paginate": {
                "previous":"上一页",
                "next": "下一页",
                "last": "末页",
                "first": "首页"
            }
        },
        "columnDefs": [
            {'orderable': false,"data":"id",'targets': [0],'Sortable': false,"visible": false},
            {'orderable': false,"data":"name",'targets': [1],'Sortable': false,"title":"栏目名称"},
            {'orderable': false,"data":"key",'targets': [2],"title":"栏目key",
                "render":function(data, type, full){
                	if(full.key===""){
                		return"<span style='color:red;'>无</span>";
                    }else{
                    	return full.key;
                    }
                }
            },{'orderable': false,"data":"starttime",'targets': [3],"title":"有效期开始时间",
                "render":function(data, type, full){
                	if(full.starttime===""){
                		return"<span style='color:red;'>无</span>";
                    }else{
                    	return full.starttime;
                    }
                }
            },{'orderable': false,"data":"endtime",'targets': [4],"title":"有效期结束时间",
                "render":function(data, type, full){
                	if(full.endtime===""){
                		return"<span style='color:red;'>无</span>";
                    }else{
                    	return full.endtime;
                    }
                }
            },{'orderable': false,"data":"creattime",'targets': [5],"title":"申请时间",
                "render":function(data, type, full){
                	if(full.creattime===""){
                		return"<span style='color:red;'>无</span>";
                    }else{
                    	return full.creattime;
                    }
                }
            },{'orderable': false,"data":"branchname",'targets': [6],"title":"申请平台",
                "render":function(data, type, full){
                	if(full.branchname===""){
                		return"<span style='color:red;'>无</span>";
                    }else{
                    	return full.branchname;
                    }
                }
            },
            {'orderable': false,"data":"branchid",'targets': [7],'Sortable': false,"visible": false},
            {'orderable': false,"data":"state",'targets': [8],'Sortable': false,"visible": false},
            {'orderable': false,'targets': [9],"title":"操作",
            	"render":function(data,type,full){
            		var btn = "<button class='btn btn-info' type='button' id='channeladoptBtn'><i class='fa fa-check'></i>通过</button>";
            		btn += "<button class='btn btn-warning' type='button' id='channelnotpassBtn'><i class='fa fa-times'></i>不通过</button>";
            		return btn;
            	}
            }
        ],
        "bAutoWidth":false,
        "bProcessing": true, // 是否显示取数据时的那个等待提示
	    "bServerSide": true,//这个用来指明是通过服务端来取数据
        "sAjaxSource":"../admin/getbranchchannel",//请求的地址
        "fnServerData": function(sSource,aoData,fnCallback){				  // 获取数据的处理函数
        	$.ajax({
        		url:sSource,
        		type : "post",
        		data:{
        			"aoData":JSON.stringify(aoData)
        		},
        		dataType : "json",
        		async:false,
        		success:function(result){
//        			result=eval("("+result+")");
        			fnCallback(result.obj);
        		},error:function(msg){
        		}
        	});
        } 
    });
	//通过申请
	table.on("click","#channeladoptBtn",function(e){
		e.preventDefault();	
    	var nRow = $(this).parents('tr');
    	var dData = oTable.fnGetData(nRow);
		$.ajax({
			url :"../admin/channel/adopt",
			type:"post",
			dataType :"Json",
			data:{id:dData.id},
			success:function(data){
				if(data.success){
					toastr.success("通过成功！");
					oTable.fnDraw(true);
				}else{
					toastr.success("操作失败！");
				}
			},error:function(a,b,c){
				if(a.readyState==4){
    				window.location.href="../../admin/loginpage";
    			}else{
    				toastr.error("系统异常！");
    			}
			}
		});
	});
	
	//不通过
	table.on("click","#channelnotpassBtn",function(e){
		e.preventDefault();	
    	var nRow = $(this).parents('tr');
    	var dData = oTable.fnGetData(nRow);
		$.ajax({
			url :"../admin/channel/notpass",
			type:"post",
			dataType :"Json",
			data:{id:dData.id},
			success:function(data){
				if(data.success){
					toastr.success("操作成功！");
					oTable.fnDraw(true);
				}else{
					toastr.success("操作失败！");
				}
			},error:function(a,b,c){
				if(a.readyState==4){
    				window.location.href="../../admin/loginpage";
    			}else{
    				toastr.error("系统异常！");
    			}
			}
		});
	});
});